package com.ruoyi.question.service.impl;

import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.question.domain.Folder;
import com.ruoyi.question.mapper.FolderMapper;
import com.ruoyi.question.service.IFolderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 目录 服务层实现
 * 
 * @author ruoyi
 * @date 2019-10-11
 */
@Service
public class FolderServiceImpl implements IFolderService
{
	@Autowired
	private FolderMapper folderMapper;

	/**
     * 查询目录信息
     * 
     * @param id 目录ID
     * @return 目录信息
     */
    @Override
	public Folder selectFolderById(Long id)
	{
	    return folderMapper.selectFolderById(id);
	}
	
	/**
     * 查询目录列表
     * 
     * @param folder 目录信息
     * @return 目录集合
     */
	@Override
	public List<Folder> selectFolderList(Folder folder)
	{
	    return folderMapper.selectFolderList(folder);
	}
	
    /**
     * 新增目录
     * 
     * @param folder 目录信息
     * @return 结果
     */
	@Override
	public int insertFolder(Folder folder)
	{
		Folder newFolder = folderMapper.selectFolderById(folder.getParentId());
		folder.setAncestors(newFolder.getAncestors()+","+folder.getParentId());
		return folderMapper.insertFolder(folder);
	}
	
	/**
     * 修改目录
     * 
     * @param folder 目录信息
     * @return 结果
     */
	@Override
	public int updateFolder(Folder folder)
	{
	    return folderMapper.updateFolder(folder);
	}

	/**
     * 删除目录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteFolderByIds(String ids)
	{
		return folderMapper.deleteFolderByIds(Convert.toStrArray(ids));
	}

	@Override
	public List<Ztree> selectFolderTree(Folder folder) {
		List<Folder> folderList = folderMapper.selectFolderList(folder);
		List<Ztree> ztrees = initZtree(folderList);
		return ztrees;
	}
	/**
	 * 对象转部门树
	 *
	 * @param deptList 部门列表
	 * @return 树结构列表
	 */
	public List<Ztree> initZtree(List<Folder> folderList)
	{
		return initZtree(folderList, null);
	}
	/**
	 * 对象转部门树
	 *
	 * @param deptList 部门列表
	 * @param roleDeptList 角色已存在菜单列表
	 * @return 树结构列表
	 */
	public List<Ztree> initZtree(List<Folder> folderList, List<String> roleDeptList)
	{

		List<Ztree> ztrees = new ArrayList<Ztree>();
		for (Folder folder : folderList)
		{
				Ztree ztree = new Ztree();
				ztree.setId(folder.getId());
				ztree.setpId(folder.getParentId());
				ztree.setName(folder.getFolderName());
				ztree.setTitle(folder.getFolderName());
				ztrees.add(ztree);
		}
		return ztrees;
	}
}
